package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacDept;
import com.csun.cmny.provider.model.dto.dept.UacEditDeptDto;
import com.csun.cmny.provider.model.vo.DeptVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacDeptService;
import com.csun.cmny.provider.service.UacEmpService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-12-13 11:17
 * @Description:
 */
@RestController
@RequestMapping(value = "/dept", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - admin - UacDeptMainController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacDeptMainController extends BaseController {

    @Resource
    private UacDeptService uacDeptService;

    @Resource
    private UacEmpService uacEmpService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    //@LogAnnotation
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增(更新)部门")
    public Wrapper<UacDept> saveDept(@RequestBody UacEditDeptDto uacEditDeptDto) {

        UacDept uacDept = new UacDept();
        LoginAuthDto loginAuthDto = getLoginAuthDto();

        BeanUtils.copyProperties(uacEditDeptDto, uacDept);

        uacDeptService.saveUacDept(uacDept, loginAuthDto);

        //return WrapMapper.ok(uacDept);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacDept);
    }

   // @LogAnnotation
    @PostMapping("/getDeptVoAll")
    @ApiOperation(httpMethod = "POST", value = "查询部门全部信息")
    public Wrapper<List<DeptVo>> getDeptVoAll() {

        List<DeptVo> deptVoList = uacDeptService.getDeptVoAll(getLoginAuthDto().getGroupId());

        //return WrapMapper.ok(deptVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), deptVoList);
    }

   // @LogAnnotation
    @PostMapping("/deleteUacDeptById/{id}")
    @ApiOperation(httpMethod = "POST", value = "删除部门")
    public Wrapper<Integer> deleteUacDeptById(@PathVariable Long id) {

        Preconditions.checkArgument(id != null, "部门Id不能为空");

        // 判断部门是否含有员工
        boolean hasEmp = uacEmpService.checkDeptHasEmp(id);
        if (hasEmp) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "此部门含有员工, 请先处理部门员工信息.");
        }

        // 判断部门是否含有子节点
        boolean hasChild = uacDeptService.checkDeptHasChildDept(id);
        if (hasChild) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "此部门含有子部门, 请先删除子部门.");
        }

        int result = uacDeptService.deleteUacDeptById(id, getLoginAuthDto());

        return super.handleResult(result);
    }

}
